/*
分页数据逻辑
待处理问题
本地缓存数据处理（如pageSize）
*/
import $ from '@/common'
export default {
  state: {
    fetching: true,
    // 查询错误状态
    fetchError: false,
    // 可指定的查询路径
    fetchPath: false,
    pageOn: 1,
    pageSize: 20,
    pageCount: 1,
    totalCount: 0,
    fetchParam: {
      pageOn: 1,
      pageSize: 20,
      filter: {}
    },
    // 当前查询条件
    filter: {
      filter: null
    },
    // 固定查询条件
    fixedFilter: {}
  },
  getters: {
    searchData(state) {
      return {}
    },
    searchPath(state) {
      return state.fetchPath || state.model.path
    }
  },
  actions: {
    fetch({state, getters, dispatch, commit},data) {
      commit('setState', {code: 'fetching',value: true})
      commit('setState', {code: 'fetchError',value: false})
      $.link(getters.searchPath, getters.searchData).then(({data}) => {
        if (data.data) {
          commit('setData', data.data)
          commit('setPageStatus', data)
        } else {
          commit('setData', data)
          commit('setPageStatus', {})
        }
      }).catch((res) => {
        console.error(res)
        commit('setState', {code: 'fetchError',value: true})
      }).finally(() => {
        commit('setState', {code: 'fetching',value: false})
      })
    },
    update({dispatch}) {
      dispatch('fetch')
    },
    search({state, dispatch}) {},
    pageTo({state, dispatch}) {},
    pageDown({state, dispatch}) {},
    pageUp({state, dispatch}) {},
    pageSizeChange() {}
  },
  mutations: {
    // 重置查询状态
    resteStatus() {},
    setPageStatus(state, data) {
      state.pageOn = data.pageOn || 1
      state.pageSize = data.pageSize || state.pageSize
      state.totalCount = data.totalCount
      state.pageCount = state.pageCount || Math.floor(state.totalCount / state.pageSize) + 1 || 1
    },
    setFetchPath(state, data) {
      state.fetchPath = data
    },
    setPageSize(state, data) {
      state.pageSize = data || state.pageSize || 20
    }
  }
}
